home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.03 Mar 89 / Basic⁄Pascal Source Code / ADB Pascal Sample / ADB.p < prev    next >
Encoding:
Text File  |  1989-02-07  |  9.6 KB  |  347 lines  |  [TEXT/PJMM]

  1. { ADBDemo }
  2. { ©1989 MacTutor}
  3. { By Dave Kelly, modified by David Smith}
  4. { This program will blink the LEDs of the Extended Keyboard on and off twice}
  5. { This has no real value other than to show how to communication with ADB devices}
  6. { NOTE: It is not clear what Apple has in mind for these LEDs, so you are using them at your own risk}
  7. { Refer to Technical Note #206 for more information.}
  8. { Other tidbits include Multifinder friendly, about dialog, vers resources, working sizable window}
  9. program ADBDemo;
  10.  
  11.     uses
  12.         OSIntf, PrintTraps, MyADBGlobals, MyADBStuff;
  13.  
  14.     procedure crash;
  15.     begin
  16.         ExitToShell;
  17.     end;
  18.  
  19.     procedure InitMac;
  20.     begin
  21.         InitGraf(@thePort);
  22.         InitFonts;
  23.         InitWindows;
  24.         InitMenus;
  25.         TEInit;
  26.         InitDialogs(@crash);
  27.         InitCursor;
  28.         FlushEvents(everyEvent, 0);
  29.         Finished := false;
  30.     end;
  31.  
  32.     procedure initRects;
  33.         var
  34.             MemoryPtr: ^Integer;
  35.     begin
  36.         MemoryPtr := pointer(mBarHeightGlobal); {System Global mBarHeight}
  37.         mBarHeight := MemoryPtr^;
  38.         screen := ScreenBits.Bounds; {current screen device}
  39.         SetRect(DragArea, Screen.left + 4, Screen.top + mBarHeight + 4, Screen.right - 4, Screen.bottom - 4);
  40.         SetRect(GrowArea, Screen.left + MinWidth, Screen.top + MinHeight, Screen.right - 8, Screen.bottom - 8);
  41.         SetRect(ADBWindowRect, Screen.left + 15, Screen.top + 45, Screen.right - 95, Screen.bottom - 95);
  42.         SetRect(ZoomRect, Screen.left + 4, Screen.top + mBarHeight + 4, Screen.right - 4, Screen.bottom - 4);
  43.     end;
  44.  
  45.     procedure InitMyWindow;
  46.         var
  47.             windtype: integer;
  48.             Visible: boolean;
  49.             GoAway: boolean;
  50.             RefVal: LongInt;
  51.             title: str255;
  52.     begin
  53.         Visible := true;
  54.         windtype := documentProc + ZoomBox;
  55.         GoAway := true;
  56.         RefVal := 0;
  57.         title := 'ADB Demo';
  58.         ADBWindow := NewWindow(nil, ADBWindowRect, title, Visible, windtype, pointer(-1), GoAway, RefVal);
  59.         ADBWindowPeek := WindowPeek(ADBWindow);
  60.         SetPort(ADBWindow);
  61.         TextFont(Geneva);
  62.         TextSize(10);
  63.         TextFace([]);    {plain}
  64.         TextMode(1);    {Or}
  65.         PenNormal;
  66.         ForeColor(blackColor);
  67.         BackColor(whiteColor);
  68.         ADBWindowPeek^.windowKind := userKind;
  69.         with ADBWindow^.portRect do
  70.             begin
  71.                 SetRect(VCRect, right - (SBarWidth - 1), top - 1, right + 1, bottom - (SBarWidth - 2));
  72.                 SetRect(HCRect, left - 1, bottom - (SBarWidth - 1), right - (SBarWidth - 2), bottom + 1);
  73.                 SetRect(GrowRect, HCRect.right, HCRect.top, VCRect.right, HCRect.bottom);
  74.                 SetRect(ViewRect, left + 4, top + 4, right - (SBarWidth - 1), bottom - (SBarWidth - 1));
  75.             end;  {of with }
  76.         DestRect := ViewRect;
  77.         myTextHandle := TENew(DestRect, ViewRect);
  78.         title := '';
  79.         VControl := NewControl(ADBWindow, VCRect, title, Visible, 0, 0, 0, ScrollBarProc, 1);
  80.         ValidRect(VCRect);
  81.         HControl := NewControl(ADBWindow, HCRect, title, Visible, 0, 0, 0, ScrollBarProc, 1);
  82.         ValidRect(HCRect);
  83.     end;
  84.  
  85.     procedure InitMyPrint;
  86.     begin
  87.         myPrint := THPrint(NewHandle(SIZEOF(TPrint)));
  88.     end;
  89.  
  90.     procedure InitMyMenus;
  91.         var
  92.             i: integer;
  93.     begin
  94.         myMenus[AppleM] := GetMenu(AppleMenu);
  95.         AddResMenu(myMenus[AppleM], 'DRVR');
  96.         myMenus[FileM] := GetMenu(FileMenu);
  97.         myMenus[EditM] := GetMenu(EditMenu);
  98.         for i := 1 to MenuCount do
  99.             InsertMenu(myMenus[i], 0);
  100.  
  101.         DisableItem(myMenus[FileM], fSave);
  102.         DisableItem(myMenus[FileM], fSaveAs);
  103.         DisableItem(myMenus[FileM], fPageSet);
  104.         DisableItem(myMenus[FileM], fPrint);
  105.         DisableItem(myMenus[EditM], eUndo);
  106.         DrawMenuBar;
  107.     end;    {of proc}
  108.  
  109.     procedure doMouse (myEvent: EventRecord);
  110.         var
  111.             whereIsIt: integer;
  112.             whichWindow: WindowPtr;
  113.             localPt, globalPt: Point;
  114.             oldPort: GrafPtr;
  115.     begin
  116.         globalPt := myEvent.where;
  117.         localPt := globalPt;        {global coord of mouse}
  118.         GlobalToLocal(localPt);     {local coord of mouse}
  119.         whereIsIt := FindWindow(globalPt, whichWindow);
  120.         case whereIsIt of
  121.             inDesk:             {0}
  122.                 doMessage('Mouse Click on Desktop.', '(Not handled in this program.)', '', '');
  123.             inMenuBar:      {1}
  124.                 doMenuBar(MenuSelect(globalPt));
  125.             inSysWindow:     {2}
  126.                 SystemClick(myEvent, whichWindow);
  127.             inContent:          {3}
  128.                 doContent(myEvent, whichWindow);
  129.             inDrag:             {4}
  130.                 doDrag(whichWindow, globalPt);
  131.             inGrow:         {5}
  132.                 doGrow(whichWindow, globalPt, False);
  133.             inGoAway:         {6}
  134.                 if TrackGoAway(whichWindow, globalPt) then
  135.                     HideWindow(whichWindow);
  136.             inZoomIn, InZoomOut:        {7, 8}
  137.                 begin
  138.                     if TrackBox(whichWindow, globalPt, whereIsIt) then
  139.                         begin
  140.                             GetPort(OldPort);
  141.                             SetPort(whichWindow); {safety device}
  142.                             EraseRect(whichWindow^.portRect);
  143.                             ZoomWindow(whichWindow, whereIsIt, True);
  144.                             doGrow(whichWindow, globalPt, True);
  145.                             SetPort(OldPort);
  146.                         end;
  147.                 end;
  148.             otherwise
  149.                 begin
  150.                 end;
  151.         end; {of whereIsIt}
  152.     end;
  153.  
  154.     procedure doKeyDowns (myEvent: EventRecord);
  155.         var
  156.             ch: char;
  157.             charCode: longInt;
  158.             keyCode: longInt;
  159.     begin
  160.         charCode := BitAnd(myEvent.Message, charCodeMask);  {strip off key code}
  161.         keyCode := BitShift(BitAnd(myEvent.Message, keyCodeMask), -8); {strip off char}
  162.         ch := Chr(charCode);  {get keyboard char}
  163.         if BitAnd(myEvent.Modifiers, CmdKey) = CmdKey then
  164.             doMenuBar(MenuKey(ch))  { do menu command key}
  165.         else
  166.             begin  { do keystroke }
  167.                 ParamText('No typing supported…', '', '', '');
  168.                 itemhit := CautionAlert(AlertDialog, nil);
  169.             end;  { of do key stroke }
  170.     end;  { of proc}
  171.  
  172.     procedure doUpdates (myEvent: EventRecord);
  173.         var
  174.             UpdateWindow: WindowPtr;
  175.             TempPort: GrafPtr;
  176.             myRect: rect;
  177.     begin
  178.         UpdateWindow := WindowPtr(myEvent.message);
  179.         if UpdateWindow = ADBWindow then
  180.             begin
  181.                 GetPort(TempPort); {save port}
  182.                 SetPort(UpdateWindow);
  183.                 BeginUpDate(UpdateWindow);
  184.                 EraseRect(UpdateWindow^.portRect);
  185.                 DrawGrowIcon(UpdateWindow);
  186.                 DrawControls(UpdateWindow);
  187.                 TEUpdate(UpdateWindow^.portRect, myTextHandle);
  188.                 EndUpDate(UpdateWindow);
  189.                 SetPort(TempPort);    {restore port}
  190.             end;
  191.     end; {of proc}
  192.  
  193.     procedure doActivates (myEvent: EventRecord);
  194.         var
  195.             TargetWindow: WindowPtr;
  196.             TargetPeek: WindowPeek;
  197.     begin
  198.         TargetWindow := WindowPtr(myEvent.message);
  199.         TargetPeek := windowPeek(TargetWindow);
  200.         SetPort(TargetWindow);
  201.         if Odd(myEvent.modifiers) then
  202.             begin {activate}
  203.                 if TargetWindow = ADBWindow then
  204.                     begin
  205.                         DisableItem(myMenus[EditM], eUndo);
  206.                         DrawGrowIcon(TargetWindow);
  207.                         TEActivate(myTextHandle);
  208.                         ShowControl(VControl);
  209.                         ShowControl(HControl);
  210.                     end
  211.             end  { of activate loop}
  212.         else
  213.             begin {deactivate}
  214.                 if TargetWindow = ADBWindow then
  215.                     begin
  216.                         EnableItem(myMenus[EditM], eUndo);
  217.                         EnableItem(myMenus[EditM], eCut);
  218.                         EnableItem(myMenus[EditM], eCopy);
  219.                         EnableItem(myMenus[EditM], ePaste);
  220.                         EnableItem(myMenus[EditM], eClear);
  221.                         DrawGrowIcon(TargetWindow);
  222.                         TEDeactivate(myTextHandle);
  223.                         HideControl(VControl);
  224.                         HideControl(HControl);
  225.                     end; { of my window activation}
  226.             end; {of deactivate loop}
  227.     end; {of proc}
  228.  
  229.     procedure doMulti (myEvent: EventRecord);
  230.  
  231.         const
  232.             MouseMovedEvt = $FA;
  233.         var
  234.             HiByte: byte;
  235.             bit0: LongInt;
  236.             sysresult: boolean;
  237.             ResumeWindow: WindowPtr;
  238.             ResumePeek: WindowPeek;
  239.             SuspendWindow: WindowPtr;
  240.             SuspendPeek: WindowPeek;
  241.             MouseMove: LongAndByte;
  242.     begin
  243.         bit0 := 31; {convert 68000 to toolbox}
  244.     {check for mouse moved event}
  245.         MouseMove.longView := myEvent.message;
  246.         HiByte := Byte(MouseMove.byteView.byte0);
  247.         if HiByte = MouseMovedEvt then
  248.             begin {Handle mouse moved event}
  249.             end;
  250.     {check for resume event }
  251.         if Odd(myEvent.message) then
  252.             begin    {resume}
  253.          { treat like activate Event if we are in front}
  254.                 ResumeWindow := FrontWindow;
  255.                 if ResumeWindow = ADBWindow then
  256.                     begin {adbWindow}
  257.                         SetPort(ResumeWindow);
  258.                         InvalRect(ResumeWindow^.portRect); { force update event}
  259.                         DisableItem(myMenus[EditM], eUndo);
  260.                         ShowControl(VControl);
  261.                         ShowControl(HControl);
  262.                         DrawGrowIcon(ResumeWindow);
  263.                     end; {adbWindow}
  264.                 if FrontWindow <> nil then
  265.                     begin {DA check}
  266.                         ResumePeek := WindowPeek(FrontWindow);
  267.                         if ResumePeek^.windowKind < 0 then {DA}
  268.                             begin {da}
  269.                                 myEvent.what := activateEvt;
  270.                                 BitSet(@myEvent.modifiers, bit0);
  271.                                 sysresult := SystemEvent(myEvent);
  272.                             end; {da}
  273.                     end; {DA check}
  274.          { end of activate Event}
  275.             end {of resume}
  276.         else
  277.             begin    {suspend}
  278.         {de-activate Event}
  279.                 SuspendWindow := FrontWindow;
  280.                 if SuspendWindow = ADBWindow then
  281.                     begin {adbwindow}
  282.                         SetPort(SuspendWindow);
  283.                         InvalRect(SuspendWindow^.portRect); {force update}
  284.                         EnableItem(myMenus[EditM], eUndo);
  285.                         EnableItem(myMenus[EditM], eCut);
  286.                         EnableItem(myMenus[EditM], eCopy);
  287.                         EnableItem(myMenus[EditM], ePaste);
  288.                         EnableItem(myMenus[EditM], eClear);
  289.                         DrawGrowIcon(SuspendWindow);
  290.                         HideControl(VControl);
  291.                         HideControl(HControl);
  292.                     end; {adbwindow}
  293.                 if FrontWindow <> nil then
  294.                     begin {DA check}
  295.                         SuspendPeek := WindowPeek(FrontWindow);
  296.                         if SuspendPeek^.windowKind < 0 then
  297.                             begin {da}
  298.                                 myEvent.what := activateEvt;
  299.                                 BitClr(@myEvent.modifiers, bit0);
  300.                                 sysresult := SystemEvent(myEvent);
  301.                             end; {da}
  302.                     end; {DA check}
  303.          { end of de-activate Event}
  304.             end; {suspend}
  305.     end; {of proc}
  306.  
  307.     procedure MainEventLoop;
  308.         const
  309.             MultiFinderEvt = 15;
  310.         var
  311.             sleep: LongInt;
  312.             Event: EventRecord;
  313.             DoIt: Boolean;
  314.     begin
  315.         sleep := 10;
  316.         repeat
  317.             TEIdle(MyTextHandle);
  318.             DoIt := WaitNextEvent(EveryEvent, Event, sleep, nil); {no mouse tracking}
  319.             if DoIt then
  320.                 case Event.what of
  321.                     mouseDown: 
  322.                         doMouse(Event);
  323.                     KeyDown, Autokey: 
  324.                         doKeyDowns(Event);
  325.                     updateEvt: 
  326.                         doUpdates(Event);
  327.                     activateEvt: 
  328.                         doActivates(Event);
  329.                     MultiFinderEvt: 
  330.                         doMulti(Event);
  331.                     otherwise
  332.                         begin
  333.                         end;
  334.                 end;    {of event case}
  335.         until Finished;     {end program}
  336.     end;
  337.  
  338. begin  { main }
  339.  
  340.     InitMac;
  341.     InitRects;
  342.     InitMyWindow;
  343.     InitMyPrint;
  344.     InitMyMenus;
  345.     MainEventLoop;
  346.  
  347. end.  { end of main }